<?php

namespace seecms\lib\builder;

use seecms\lib\builder\column\ActionColumn;

class Page
{

    /**
     * 数据库表名称
     * @var string
     */
    protected $table_name = '';

    /**
     * 页面标题
     * @var string
     */
    protected $title = '';

    /**
     * @var string|array
     */
    protected $dictionary = '';

    /**
     * 页面提醒
     * @var array|string
     */
    protected $alerts = [];

    /**
     * 左侧工具栏按钮
     * @var array
     */
    protected $left_toolbars = ['create', 'del'];

    /**
     * 右侧工具栏按钮
     * @var array
     */
    protected $right_toolbars = ['filter', 'print', 'exports'];

    /**
     * 搜索字段
     * @var array
     */
    protected $query = [];

    /**
     * 表格字段
     * @var array
     */
    protected $columns = [];

    /**
     * 表格中的操作按钮
     * @var array
     */
    protected $actions = ['read', 'edit', 'del'];

    /**
     * 资源路由
     * @var array
     */
    protected $resource_route = [];

    /**
     * @var string
     */
    protected $route_name = '';

    public function __construct($title = '', $table_name = null)
    {
        $this->title = $title;
        $this->table_name = $table_name;
    }

    /**
     * @param string $title
     * @return $this
     */
    public function setTitle(string $title)
    {
        $this->title = $title;

        return $this;
    }

    /**
     * @return string
     */
    public function getTitle(): string
    {
        return $this->title ?: '';
    }

    /**
     * 获取表名称
     * @return string
     */
    public function getTableName(): string
    {
        return $this->table_name ?: '';
    }


    /**
     * @param array|string $dictionary
     * @return $this
     */
    public function setDictionary($dictionary)
    {
        $this->dictionary = $dictionary;

        return $this;
    }

    /**
     * @return array|string
     */
    public function getDictionary()
    {
        return $this->dictionary;
    }

    /**
     * @param $alerts
     * @return $this
     */
    public function setAlerts($alerts)
    {
        $this->alerts = $alerts;

        return $this;
    }

    /**
     * @return array|string
     */
    public function getAlerts()
    {
        return $this->alerts;
    }

    /**
     * @param  $query
     * @return $this
     */
    public function setQuery($query)
    {
        $this->query = $query;

        return $this;
    }

    /**
     * @return array
     */
    public function getQuery(): array
    {
        return $this->query;
    }

    /**
     * @param array $columns
     * @return $this
     */
    public function setColumns(array $columns): Page
    {
        $this->columns = $columns;

        return $this;
    }

    /**
     * @return array
     */
    public function getColumns(): array
    {
        return $this->columns;
    }


    /**
     * @param array $actions
     * @return $this
     */
    public function setActions(array $actions): Page
    {
        $this->actions = $actions;

        return $this;
    }

    /**
     * @return array
     */
    public function getActions(): array
    {
        return $this->actions;
    }

    /**
     * 获取包含操作的列
     * @return array
     */
    public function getActionColumns(): array
    {
        $columns = $this->getColumns();

        if ($this->getActions()) {
            $columns[] = ActionColumn::init($this->getActions());
        }

        return $columns;
    }


    /**
     * @param array $toolbars
     * @return $this
     */
    public function setLeftToolbars(array $toolbars): Page
    {
        $this->left_toolbars = $toolbars;

        return $this;
    }

    /**
     * @return array
     */
    public function getLeftToolbars(): array
    {
        return $this->left_toolbars;
    }

    /**
     * @param array $toolbars
     * @return $this
     */
    public function setRightToolbars(array $toolbars): Page
    {
        $this->right_toolbars = $toolbars;

        return $this;
    }

    public function getRightToolbars(): array
    {
        return $this->right_toolbars;
    }

    /**
     * @return string
     */
    public function getRouteName(): string
    {
        return $this->route_name;
    }

    /**
     * @param string $route_name
     * @return $this
     */
    public function setRouteName(string $route_name): Page
    {
        $this->route_name = $route_name;

        return $this;
    }

    /**
     * @param $route
     * @return void
     */
    public function setResourceRoute($route)
    {
        $this->resource_route = $route;
    }

    public function getResourceRoute(): array
    {
        if ($this->resource_route) {
            return $this->resource_route;
        }

        $resource_route = [
            'index',
            'read',
            'create',
            'edit',
            'update',
            'delete',
            'patch',
        ];

        $routes = [];
        foreach ($resource_route as $route) {
            $routes[$route] = auth_route($this->route_name, $route);
        }

        return $routes;
    }
}